$(document).ready(function(){
    console.log($(window).height(), $('.generate').width());
    // $('.page_seven').height($(window).height());
    // $('.tanmu').height($(window).height() * 0.8)
    // $('.input_box').height($(window).height() * 0.15)
    $('.inputbg input').height($(window).width() * 0.80 *0.15/0.95);
    var scenebhArr = ['db','bj','zg','mg','nj'],
        sceneid = 0,
        ranId = Math.random().toString() + new Date().getTime().toString(),
        tplIdArr = ["2064", "2062", "2098", "2099", "2063"],
        danmuArr = [
            "广之旅37岁生日快乐ヽ(ﾟ∀ﾟ)ﾒ(ﾟ∀ﾟ)ﾉ",
            "666666666666 ",
            "深耕旅游37载！广之旅，你值得信赖！",
            "！！！火速围观！！！",
            "为你爆灯！为你转身！为你打call！日夜不分！",
            "跟着广之旅向世界出发！品质旅游说走就走！",
            "前方高能预警！",
            "( • ω• )✧ 旅界大V实力派，广之旅冇得弹！",
            "吹蜡烛，切蛋糕！Happy birthday to 广之旅！",
            "手动比心！",
            "来晚了！祝广之旅生日快乐！再创新高！"
        ],
        daunum = 0,
        bprew = true,
        bbir = true;

    //准备
	$('.prepare_box').click(function(){
        console.log(111);
        // $(".gzl_bg .page_two").addClass('showpage').prev().css("z-index", function(){
        //     console.log('inner');
        //     $('.loadding_inner').addClass('loadding_inner_run');
        //     setTimeout(function () {
        //         console.log('setTimeout');
        //         $(".gzl_bg .page_four").addClass('showpage').prev().css({
        //             "z-index": "-1"
        //         });
        //     }, 2500)
        //     return "-1"
        // });

        $(".gzl_bg .page_two").addClass('showpage').find('.loadding_inner').addClass('loadding_inner_run');
        // $('.loadding_inner').addClass('loadding_inner_run');
        setTimeout(function () {
            console.log('setTimeout');
            $(".gzl_bg .page_four").addClass('showpage');
            setTimeout(function(){
                $('.choose_tip').hide();
            },1500)
        },2500)
        
	})



    // 下一页
    $('.next_page').each(function(ind){
        var name;
        if (ind == 0) {
            name = 'five';
        }else{
            name = 'six';
        }
        $(this).click(function(){
            console.log(ind);
            $(".gzl_bg .page_"+name).addClass('showpage');
        })
    })

    //输入祝福语
    $('.enter_greeting').click(function () {
        $(".gzl_bg .page_seven").addClass('showpage').prev().css("z-index", function(){
            var timer = setInterval(function () {
                if (daunum == danmuArr.length) {
                    clearInterval(timer);
                } else {
                    // var span = $("<span class='spanbg" + Math.ceil(Math.random() * 2) + "'>" + danmuArr[daunum] + "</span>"); //<img class='loveimg' src='../image/love" + Math.ceil(Math.random() * 2) +".png'>
                    var _top = Math.random() * 80;
                    $('.spanbg').eq(daunum).css({
                        "top": _top + '%'
                    }).addClass('scrollleft spanbg' + Math.ceil(Math.random() * 2));
                    daunum++;
                }
            }, 1500)
            return "-1"
        });
    })


    //下一个背景图
    // $('.next').click(function(){       
    //     if (sceneid == 4) {
    //         sceneid = 0;
    //     }else{
    //         sceneid++;
    //     }
    //     console.log(scenebhArr[sceneid]);
    //     $('.five_choose .swiper-slide').eq(sceneid).addClass('current').siblings().removeClass('current');
    //     $('.five_tit').attr('src',"../image/"+ scenebhArr[sceneid] +".png");
    //     $('.six_pic_txt').attr('src',"../image/"+ scenebhArr[sceneid] +"t.png");
    // });

    // //上一个背景图
    // $('.prev').click(function(){
    //     if (sceneid == 0) {
    //         sceneid = 4;
    //     }else{
    //         sceneid--;
    //     }
    //     console.log(scenebhArr[sceneid]);
    //     $('.five_choose .swiper-slide').eq(sceneid).addClass('current').siblings().removeClass('current');
    //     $('.five_tit').attr('src',"../image/"+ scenebhArr[sceneid] +".png");
    //     $('.six_pic_txt').attr('src',"../image/"+ scenebhArr[sceneid] +"t.png");
    // })
    
    var mySwiper = new Swiper('.swiper-container', {
        speed: 600,
        slidesPerView: "auto",
        centeredSlides: true,
        prevButton: '.swiper-button-prev',
        nextButton: '.swiper-button-next',
        // followFinger: false,
        onSlideChangeEnd: function (swiper){
            console.log(swiper.activeIndex);
            sceneid = swiper.activeIndex;
            $('.five_tit').attr('src', "../image/" + scenebhArr[sceneid] + ".png");
            $('.six_pic_txt').attr('src',"../image/"+ scenebhArr[sceneid] +"t.png");
        }
        
    })


    //文件发生改变
    $('.upload_input').change(function(){
        console.log('bprew',bprew);
        var image = $("#havepicFile")[0].files; 
        if (bprew) {
            console.log(bprew);
            bprew = false;
            if (image === null || image === undefined) {
                $('.uploadnotbox').show();
                setTimeout(function () {
                    $('.uploadnotbox').hide();
                    bprew = true;
                }, 500)
            } else {
                untils.birPreview(ranId, tplIdArr[sceneid], function (data) {
                    $("#havepicFile").val('');
                    if (data.success) {
                        $('.sence').attr('src', 'data:image/png;base64,' + data.responseObject);
                        $('.six_pic').attr('src', 'data:image/png;base64,' + data.responseObject);
                        $('.five_next').show();
                        bprew = true;
                    }else{
                        if (data.desc == "system busy, please try again later") {
                            $('.uploaderrbox .uploadtxt').text('系统繁忙，请稍后重试！');
                            // $('.uploaderrbox').show();
                            // setTimeout(function () {
                            //     bprew = true;
                            //     $('.uploaderrbox').hide();
                            // }, 2000)
                        } else {
                            $('.uploaderrbox .uploadtxt').text('上传失败，请重新上传');
                        }
                        $('.uploaderrbox').show();
                        setTimeout(function () {
                            bprew = true;
                            $('.uploaderrbox').hide();
                        }, 2000)
                    }
                    
                });
            }
        }
    })

    //重新制作
    $('.renew').click(function() {
        console.log($("#havepicFile")[0].files, typeof ($("#havepicFile")[0].files));
        console.log($("#havepicFile").val());
        $("#havepicFile").val('');
        console.log($("#havepicFile").val());
        $('.five_next').hide();
        $('.sence').attr('src', '../image/monkey.png' );
        $('.six_pic').attr('src', ''); 
        $(".gzl_bg .page_four").removeClass('showpage').siblings('.page_five').removeClass('showpage').siblings('.page_six').removeClass('showpage');
        $(".gzl_bg .page_four").addClass('showpage');
    })


    //合成
    $('.generate').click(function(){
        var txt = $(".inputbg input").val();
        if (txt) {            
            $('.final_container').show('fast');
            untils.birBuild(ranId, sceneid + 1, txt+' ',function(data) {
                if (data.success) {                    
                    $('.final_pic').attr('src', 'data:image/png;base64,' + data.responseObject); 
                    $('.downloadpic').attr('href', 'data:image/png;base64,' + data.responseObject);
                    $('.final_box').show('fast',function() {                       
                        $('.final_pic').animate({
                            'top': '10%'
                        }, '2000');
                    })
                }else{
                    $('.final_txt').show();
                }
            });
        }else{
            alert('请先输入祝福语哦！');
        }
    })
})